package com.huawei.petSystemdemo.ui.mOrder;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.adapter.OrderAdapter;
import com.huawei.petSystemdemo.adapter.OrderShopAdapter;
import com.huawei.petSystemdemo.bean.Order;
import com.huawei.petSystemdemo.data.OrderRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class OrderActivity extends AppCompatActivity {
    private TextView order_all;
    private TextView order_wait_pay;
    private TextView order_fahuo;
    private TextView order_shouhuo;
    private List<Order> orderList;
    private TextView noOrders;
    private ImageView order_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        GlobalViewModel global = GlobalViewModel.getGlobal();

        Intent intent = getIntent();
        int style = intent.getIntExtra("name", -1);

        RecyclerView list = findViewById(R.id.order_recycle);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        list.setLayoutManager(manager);

        //获得元素
        order_all = findViewById(R.id.order_all);
        order_wait_pay = findViewById(R.id.order_wait_pay);
        order_fahuo = findViewById(R.id.order_fahuo);
        order_shouhuo = findViewById(R.id.order_shouhuo);
        noOrders = findViewById(R.id.noOrders);
        order_back = findViewById(R.id.order_back);

        order_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //初始化
        OrderRepository.getOrderByState(style, global.user.getId(), new Consumer<List<Order>>() {
            @Override
            public void accept(List<Order> orders) {
                orderList = new ArrayList<>();
                orderList.addAll(orders);
                OrderAdapter adapter = new OrderAdapter(orderList, getBaseContext());
                list.setAdapter(adapter);
                if(orders.size() == 0) {
                    noOrders.setVisibility(View.VISIBLE);
                } else {
                    noOrders.setVisibility(View.GONE);
                }
            }
        });

        order_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderRepository.getOrder(global.user.getId(), new Consumer<List<Order>>() {
                    @Override
                    public void accept(List<Order> orders) {
                        orderList = new ArrayList<>();
                        orderList.addAll(orders);
                        OrderAdapter adapter = new OrderAdapter(orderList, getBaseContext());
                        list.setAdapter(adapter);
                        if(orders.size() == 0) {
                            noOrders.setVisibility(View.VISIBLE);
                        } else {
                            noOrders.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });

        order_wait_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderRepository.getOrderByState(0, global.user.getId(), new Consumer<List<Order>>() {
                    @Override
                    public void accept(List<Order> orders) {
                        orderList = new ArrayList<>();
                        orderList.addAll(orders);
                        OrderAdapter adapter = new OrderAdapter(orderList, getBaseContext());
                        list.setAdapter(adapter);
                        if(orders.size() == 0) {
                            noOrders.setVisibility(View.VISIBLE);
                        } else {
                            noOrders.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });

        order_fahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderRepository.getOrderByState(1, global.user.getId(), new Consumer<List<Order>>() {
                    @Override
                    public void accept(List<Order> orders) {
                        orderList = new ArrayList<>();
                        orderList.addAll(orders);
                        OrderAdapter adapter = new OrderAdapter(orderList, getBaseContext());
                        list.setAdapter(adapter);
                        if(orders.size() == 0) {
                            noOrders.setVisibility(View.VISIBLE);
                        } else {
                            noOrders.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });

        order_shouhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderRepository.getOrderByState(2, global.user.getId(), new Consumer<List<Order>>() {
                    @Override
                    public void accept(List<Order> orders) {
                        orderList = new ArrayList<>();
                        orderList.addAll(orders);
                        OrderAdapter adapter = new OrderAdapter(orderList, getBaseContext());
                        list.setAdapter(adapter);
                        if(orders.size() == 0) {
                            noOrders.setVisibility(View.VISIBLE);
                        } else {
                            noOrders.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });
    }
}